package com.itheima.service.Impl;

import com.itheima.entity.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;
    /**
     * 作用： 登陆校验
     * @param
     * @return
     */
    @Override
    public User login(String phone, String code, String codeInSession) {
        //1. 校验验证码是否为空并且两个验证码是否一致
        User dbUser = null;
        if(codeInSession!=null && code.equalsIgnoreCase(codeInSession)){
            //验证码没有问题
            //2. 根据手机号查询用户，
            dbUser =  userMapper.findByPhone(phone);
            if(dbUser==null){
                //3. 如果为空代表该用户是新用户，直接注册
                dbUser = new User();
                dbUser.setPhone(phone);
                dbUser.setStatus(1);
                userMapper.save(dbUser);
            }
        }
        return dbUser;
    }
}


